
/*    This file is part of the Managarm operating system.
 *   Copyright (C) 2007, 2008, 2009  Alexander van der Grinten
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

package org.managarm.jmanacclib.core;

import java.io.Reader;
import java.io.PushbackReader;
import java.io.IOException;

public abstract class CclibReader {
	public static final class FileReader extends CclibReader {
		private final CclibSource source;
		private final PushbackReader reader;
		private int line = 0;
		
		public FileReader(CclibSource source, Reader reader) {
			this.source = source; this.reader = new PushbackReader(reader);
		}
		
		// implementation of abstract methods
		@Override public boolean endReached() throws CclibException {
			try {
				final int c = reader.read();
				if(c == -1)
					return true;
				reader.unread(c);
				return false;
			} catch(IOException e) {
				throw new CclibException("Could not read source file", source);
			}
		}
		@Override public char fetch() throws CclibException {
			try {
				final char c = (char)reader.read();
				if(c == '\n')
					line++;
				return c;
			} catch(IOException e) {
				throw new CclibException("Could not read source file", source);
			}
		}
		@Override public CclibSource getSource() {
			return source;
		}
		@Override public int getDisplacement() {
			return line;
		}
	}
	
	public static final class StringReader extends CclibReader {
		private final CclibSource source;
		private final String string;
		private int position = 0;
		private int line = 0;
		
		public StringReader(CclibSource source, String string) {
			this.source = source; this.string = string;
		}
		
		// implementation of abstract methods
		@Override public boolean endReached() throws CclibException {
			return position == string.length();
		}
		@Override public char fetch() throws CclibException {
			final char c = string.charAt(position);
			if(c == '\n')
				line++;
			position++;
			return c;
		}
		@Override public CclibSource getSource() {
			return source;
		}
		@Override public int getDisplacement() {
			return line;
		}
	}
	
	public abstract boolean endReached() throws CclibException;
	public abstract char fetch() throws CclibException;
	public abstract int getDisplacement() throws CclibException;
	public abstract CclibSource getSource() throws CclibException;
	
	public final CclibUnit getUnit() throws CclibException {
		return getSource().unit;
	}
	public final int getLine() throws CclibException{
		return getSource().line + getDisplacement();
	}
}
